<template>
  <div>
    <el-row :gutter="20">
      <!--状态-->
      <el-col :span="5">
        <detection></detection>
      </el-col>
      <!--科室-->
      <el-col :span="19">
        <department></department>
      </el-col>
    </el-row>
    <div class="container">
      <!--同步选项-->
      <div class="option">
        <el-radio-group v-model="radio">
          <el-radio :label="1">药物临床</el-radio>
          <el-radio :label="6">中文临床</el-radio>
          <el-radio :label="7">英文临床</el-radio>
          <el-radio :label="2">万方论文</el-radio>
          <el-radio :label="3">英文论文</el-radio>
          <el-radio :label="4">中文共著</el-radio>
          <el-radio :label="5">英文共著</el-radio>
        </el-radio-group>
      </div>
      <!--同步-->
      <div>
        <el-row :gutter="20">
          <el-col :span="16">
            <el-input v-model="startA" class="number_input" size="mini"></el-input>
            <span style="margin: 0 5px">-</span>
            <el-input v-model="endA" class="number_input" size="mini"></el-input>
            <el-button type="primary" size="mini" class="begin" :disabled="begina" @click="beginA">同步A</el-button>
            <el-checkbox v-model="checked">是否清除数据</el-checkbox>
            <span v-if="messageA =='同步成功！！！' " style="color: #00a854;">{{messageA}}</span>
            <span v-else style="color: red;">{{messageA}}</span>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" size="mini" class="begin" :disabled="beginb" @click="beginB">开始B</el-button>
            <span v-if="messageB =='同步成功！！！' " style="color: #00a854;">{{messageB}}</span>
            <span v-else style="color: red;">{{messageB}}</span>
          </el-col>
        </el-row>
      </div>
      <!--表格-->
      <div style="margin-top: 10px">
        <el-date-picker
          v-model="date"
          type="date"
          placeholder="选择日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd">
        </el-date-picker>
        <el-table :data="tableData" :default-sort="{prop: 'time', order: 'descending'}" border
                  style="width: 100%;margin-top: 10px"
                  :header-cell-style="{color:'#606266'}">
          <el-table-column label="时间" sortable width="140px" prop="time"></el-table-column>
          <el-table-column label="操作" prop="description"></el-table-column>
          <el-table-column label="数量" width="60px" prop="count"></el-table-column>
          <el-table-column label="表" width="240px" prop="table"></el-table-column>
          <el-table-column label="状态" width="90px" prop="status"></el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 删除提示框 -->
    <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
      <div class="del-dialog-cnt">该操作会删除上次数据，是否继续？</div>
      <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="determine">确 定</el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
  import detection from '../element/detection';
  import department from '../element/department';

  export default {
    components: {detection, department},
    data() {
      return {
        /*同步选项*/
        radio: 1,
        table: '',
        source: '',
        /*A*/
        startA: '',
        endA: '',
        checked: true,
        begina: false,
        messageA: '',
        /*B*/
        beginb: false,
        messageB: '',
        /*日期*/
        date: '',
        /*表格数据*/
        tableData: [],
        /*确认删除*/
        delVisible: false,
        eliminate: '',
        /*api*/
        apiA: '',
        apiB: '',
      }
    },
    watch: {
      radio() {
        this.option();
        this.messageA = '';
        this.messageB = '';
        this.dataPost(this.$com.ZRUrl + '/huimeidatalog/select', {data: this.date, table: this.table})
      },
      date() {
        this.option();
        this.messageA = '';
        this.messageB = '';
        this.dataPost(this.$com.ZRUrl + '/huimeidatalog/select', {data: this.date, table: this.table})
      }
    },
    created() {
      this.date = this.time();
    },
    methods: {
      /*A*/
      beginA() {
        if (this.startA.replace(/[^\d]/g, '') && this.endA.replace(/[^\d]/g, '')) {
          if (this.startA < this.endA) {
            if (this.checked == true) {
              this.eliminate = 1;
              this.delVisible = true;
            } else {
              this.eliminate = 0;
              this.determine()
            }
          } else {
            this.$message.warning('请正确输入');
          }
        } else {
          this.$message.warning('请输入范围');
        }
      },
      determine() {
        this.delVisible = false;
        this.begina = true;
        this.beginb = true;
        this.messageA = '';
        if (this.radio == 1) {
          this.source = 1;
        } else if (this.radio == 6) {
          this.source = 3;
        } else if (this.radio == 7) {
          this.source = 2;
        }
        let data = {
          startIndex: Number(this.startA),
          endIndex: Number(this.endA),
          isTruncate: this.eliminate,
          dataType: this.source,
        };
        this.setTimeout();
        this.synchronizationA(this.apiA, data)
      },
      synchronizationA(api, data) {
        this.$axios({
          method: 'POST',
          headers: {'Content-Type': 'application/json;charset=UTF-8'},
          url: this.$com.UrlYogether + api,
          data: JSON.stringify(data),
        }).then((res) => {
          if (res.data.code = 200) {
            this.messageA = res.data.data;
          } else {
            this.messageA = '同步出错！';
          }
          this.begina = false;
          this.beginb = false;
          this.stopSetTimeout();
        })
      },
      /*B*/
      beginB() {
        this.begina = true;
        this.beginb = true;
        this.messageB = '';
        this.setTimeout();
        this.synchronizationB(this.apiB)
      },
      synchronizationB(api) {
        this.$axios({
          method: 'GET',
          url: this.$com.UrlYogether + api,
        }).then((res) => {
          if (res.data.code = 200) {
            this.messageB = res.data.data;
          } else {
            this.messageB = '同步出错！';
          }
          this.begina = false;
          this.beginb = false;
          this.stopSetTimeout();
        })
      },
      /*同步选项*/
      option() {
        if (this.radio == 1) {
          this.table = 'EsPhysicianClinicalTrial';
          this.apiA = '/clinical/master';
          this.apiB = '/clinical/cluster';
        } else if (this.radio == 2) {
          this.table = 'EsPhysicianPublication';
          this.apiA = '/paper/master';
          this.apiB = '/paper/cluster';
        } else if (this.radio == 3) {
          this.table = 'EsPhysicianPublicationPubmed';
          this.apiA = '/paper/master/en';
          this.apiB = '/paper/cluster';
        } else if (this.radio == 4) {
          this.table = 'EsPhysicianPublicationCoauthor';
          this.apiA = '/total/master';
          this.apiB = '/total/syn';
        } else if (this.radio == 5) {
          this.table = 'EsPhysicianPublicationCoauthorPubmed';
          this.apiA = '/total/cluster';
          this.apiB = '/total/syn';
        } else if (this.radio == 6) {
          this.table = 'EsPhysicianClinicalTrial';
          this.apiA = '/clinical/master';
          this.apiB = '/clinical/cluster';
        } else if (this.radio == 7) {
          this.table = 'EsPhysicianClinicalTrial';
          this.apiA = '/clinical/master';
          this.apiB = '/clinical/cluster';
        }
      },
      /*定时器*/
      setTimeout() {
        this.timer = setInterval(() => {
          this.dataPost(this.$com.ZRUrl + '/huimeidatalog/select', {data: this.date, table: this.table})
        }, 10000);
      },
      /*停止定时器*/
      stopSetTimeout() {
        clearInterval(this.timer);
        setTimeout(() => {
          this.dataPost(this.$com.ZRUrl + '/huimeidatalog/select', {data: this.date, table: this.table})
        }, 1000);
      },
      /*当前时间*/

      time() {
        let year = new Date().getFullYear();
        let month = new Date().getMonth() + 1 < 10 ? "0" + (new Date().getMonth() + 1) : new Date().getMonth() + 1;
        let date = new Date().getDate() < 10 ? "0" + new Date().getDate() : new Date().getDate();
        let hh = new Date().getHours() < 10 ? "0" + new Date().getHours() : new Date().getHours();
        let mm = new Date().getMinutes() < 10 ? "0" + new Date().getMinutes() : new Date().getMinutes();
        return year + "-" + month + "-" + date;
      },
      /*表格数据*/
      dataPost(Url, data) {
        this.$axios({
          method: 'POST',
          headers: {'Content-Type': 'application/json;charset=UTF-8'},
          url: Url,
          data: JSON.stringify(data),
        }).then((res) => {
          if (res.data.code = 200) {
            this.tableData = res.data.result;
          }
        })
      },
    },
  }
</script>

<style scoped>
  .container {
    padding: 20px;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 5px;
    margin-bottom: 15px;
    font-size: 15px;
  }

  /*同步选项*/
  .container .option {
    margin-bottom: 10px;
  }
  /*同步*/
  .container .number_input {
    width: 100px;
    display: inline-block;
  }
  .container .begin {
    width: 80px;
  }
</style>
